package com.ctl.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IndexServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = -9203033698231557339L;

    public IndexServlet() {
        super();
    }

    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        SelectMethod db = new SelectMethod();
        List<Student> list_student = db.getStudents("select * from student");
        PageBean pagebean = new PageBean(list_student.size());

        int pagesize = pagebean.getPageSize();// 获取分页大小
        String action = request.getParameter("action");// 从页面获取操作状态
        String curPage = request.getParameter("page");
        int page = Integer.parseInt(curPage);
        switch (action) {
            case "shang":
                page -= 1;
                if (page < 1) {
                    page = 1;
                }
                break;
            case "xia":
                page += 1;
                if (page > pagebean.getPageCount()) {
                    page = pagebean.getPageCount();
                }
                break;
            default:
                break;
        }
        int firstIndex = (page - 1) * pagesize;
        int toIndex = (page) * pagesize;
        //判断toIndex是否越界
        if(toIndex>list_student.size()){
            toIndex=list_student.size();
        }
        switch (action) {
            case "first":
                pagebean.setCurPage(page);
                // 获取分页数据在List集合中的索引
                break;
            case "last":
                // 获取分页数据在List集合中的索引
                if (toIndex > list_student.size()) {
                    toIndex = list_student.size();
                }
                pagebean.setCurPage(page);
                break;
            case "xia":
                if (page < pagebean.getPageCount()) {
                    pagebean.setCurPage(page);// 设置当前页

                } else {
                    if (toIndex > list_student.size()) {
                        toIndex = list_student.size();
                    }
                    pagebean.setCurPage(page);
                }
                break;
            case "shang":
                if (page >= 1) {
                    pagebean.setCurPage(page);// 设置当前页
                    // 获取分页数据在List集合中的索引
                    /*                 int firstIndex = (page - 1) * pagesize;
                                        int toIndex = (page) * pagesize;
                    */
                } else {
                    if (toIndex > list_student.size()) {
                        toIndex = list_student.size();
                    }
                    pagebean.setCurPage(page);
                }
                break;
            default:
                break;
        }
        // 截取数据集合，获取分页数据
        List<Student> list_student_cut = list_student.subList(firstIndex, toIndex);
        request.getSession().setAttribute("list_student", list_student_cut);
        request.getSession().setAttribute("pageBean", pagebean);
        response.sendRedirect("index.jsp");
    }

    public void init() throws ServletException {
        // Put your code here
    }

}
